import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/4 9:00
 * @description 双指针
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.discountPrices("ka3caz4837h6ada4 r1 $602", 9);
    }
    public String discountPrices(String sentence, int discount) {
        StringBuffer sb = new StringBuffer();
        int n = sentence.length(), i = 0, j = 0;
        char[] sen = sentence.toCharArray();
        while (j < n) {
            // 截取i到j的一段连续单词
            i = j;
            boolean allNumber = true;
            while (j < n && sen[j] != ' ') {
                if (j > i && !Character.isDigit(sen[j])) {
                    allNumber = false;
                }
                j++;
            }
            // 如果初始为美元符号并且这一段都是数字且$后面有数字，需要打折
            if (sen[i] == '$' && allNumber && i + 1 != j) {
                String mo = sentence.substring(i + 1, j);
                long money = 0;
                int m = mo.length(), mul = 1;
                for(int k = m - 1; k >= 0; k--) {
                    money += (long) (mo.charAt(k) - '0') * mul;
                    mul *= 10;
                }
                money *= (100 - discount);
                sb.append('$').append(money / 100).append('.');
                money %= 100;
                if (money == 0 || money < 10) {
                    sb.append('0');
                }
                sb.append(money);
            }else {
                sb.append(sentence, i, j);
            }
            sb.append(" ");
            j++;
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }
}
